package com.yonyou.gmmc.service.pointactivity.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.MultipartConfigElement;
import javax.servlet.http.HttpServletRequest;
import com.yonyou.gmmc.service.pointactivity.model.*;
import com.yonyou.gmmc.service.pointactivity.service.TtWinnerRecordService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.yonyou.gmmc.service.pointactivity.constant.PointActivityConstant;
import com.yonyou.gmmc.service.pointactivity.service.SendMessageService;
import com.yonyou.gmmc.service.pointactivity.util.MailJob;
import io.swagger.annotations.Api;

/*
 * 发送信息
 * @author 徐庆锋
 */
@RestController
@RequestMapping(value = PointActivityConstant.BASE_PATH + "/" + PointActivityConstant.VERSION)
@Api("发送邮件API")
public class SendMessageController {

	private Logger logger = Logger.getLogger(SendMessageController.class);
	@Autowired
	SendMessageService sendMessageService;
	@Autowired
	private TtWinnerRecordService ttWinnerRecordService;

	@Bean
	MultipartConfigElement multipartConfigElement() {
		MultipartConfigFactory factory = new MultipartConfigFactory();
		factory.setMaxFileSize("10240KB");
		factory.setMaxRequestSize("10240KB");
	    factory.setLocation("/home/lyh_admin/appfile/fs01");
	//	factory.setLocation("D:/fs02"); // 上传文件临时目录
		return factory.createMultipartConfig();
	}

	/**
	 * 通过前端页面发送邮件
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/sendEmail", method = RequestMethod.POST)
	public String sendEmail(HttpServletRequest request) throws Exception {
		logger.info(this.getClass().getName() + ".sendEmail():start");
		String content = request.getParameter("content"); // 邮件正文内容
		String title = request.getParameter("title"); // 邮件标题
		String stateCode = "1"; // 发送邮件正常

		// 上传附件
		List<Map<String, Object>> listOriginal = null; // 附件集合
		try {
			listOriginal = sendMessageService.uploadFile(request);
		} catch (Exception e1) {
			e1.printStackTrace();
			return stateCode = "2";// 上传附件异常
		}

		// 获取发件人信息
		TmEmailSend tmEmailSend = sendMessageService.getEmailSendInfo();
		String sendEmail = tmEmailSend.getSendEmail();
		String password = tmEmailSend.getPassword();
		String smtpserver = tmEmailSend.getSmtpserver();

		// 获取收件人信息
		List<TmEmailAccept> list = sendMessageService.getEmailAcceptInfo();
		String acceptEmail = null;
		List<String> acceptEmailList = new ArrayList<String>(); // 收件人集合
		for (TmEmailAccept tmEmailAccept : list) {
			acceptEmail = tmEmailAccept.getAcceptEmail();
			acceptEmailList.add(acceptEmail);
		}

		// 记录邮件日志信息
		TmEmailLog tmEmailLog = new TmEmailLog();
		tmEmailLog.setSendEmail(sendEmail);
		tmEmailLog.setState(1);
		tmEmailLog.setCreateBy(1);

		try {
			// 发送邮件
			MailJob.execute(content, title, sendEmail, password, smtpserver, acceptEmailList, listOriginal);
		} catch (Exception e) {
			e.printStackTrace();
			tmEmailLog.setState(0);
			stateCode = "3"; // 发送邮件异常
		}

		sendMessageService.saveEmailLogInfo(tmEmailLog, acceptEmailList); // 保存邮件日志
		sendMessageService.saveEmailAcceptInfo(acceptEmailList); // 保存收件信息
		logger.info(this.getClass().getName() + ".sendEmail():end");
		return stateCode;
	}

	/**
	 * 通过java方法调用发送邮件
	 * 
	 * @param content
	 * @param title
	 * @param listFile
	 * @return
	 */
	public String sendEmail(String content, String title, List<File> listFile) throws Exception {
		logger.info(this.getClass().getName() + ".sendEmail2():start");
		String stateCode = "1"; // 发送邮件正常
		// 上传附件
		List<Map<String, Object>> listOriginal = null; // 附件集合
		try {
			listOriginal = sendMessageService.uploadFile(listFile);
		} catch (Exception e1) {
			e1.printStackTrace();
			return stateCode = "2";// 上传附件异常
		}

		// 获取发件人信息
		TmEmailSend tmEmailSend = sendMessageService.getEmailSendInfo();
		String sendEmail = tmEmailSend.getSendEmail();
		String password = tmEmailSend.getPassword();
		String smtpserver = tmEmailSend.getSmtpserver();

		// 获取收件人信息
		List<TmEmailAccept> list = sendMessageService.getEmailAcceptInfo();
		String acceptEmail = null;
		List<String> acceptEmailList = new ArrayList<String>(); // 收件人集合
		for (TmEmailAccept tmEmailAccept : list) {
			acceptEmail = tmEmailAccept.getAcceptEmail();
			acceptEmailList.add(acceptEmail);
		}

		// 记录邮件日志信息
		TmEmailLog tmEmailLog = new TmEmailLog();
		tmEmailLog.setSendEmail(sendEmail);
		tmEmailLog.setState(1);
		tmEmailLog.setCreateBy(1);

		try {
			// 发送邮件
			MailJob.execute2(content, title, sendEmail, password, smtpserver, acceptEmailList, listOriginal);
		} catch (Exception e) {
			e.printStackTrace();
			tmEmailLog.setState(0);
			stateCode = "3"; // 发送邮件异常
		}

		sendMessageService.saveEmailLogInfo(tmEmailLog, acceptEmailList); // 保存邮件日志
		sendMessageService.saveEmailAcceptInfo(acceptEmailList); // 保存收件信息
		logger.info(this.getClass().getName() + ".sendEmail2():end");
		return stateCode;
	}

	/**
	 * 通过内容和手机号码发送短信
	 * @param phone
	 * @param content
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/sendSms", method = RequestMethod.GET)
	public String sendSms(@RequestParam("phone") String phone, @RequestParam("content") String content) throws Exception {
		String stateCode = "1"; // 发短信成功
		try {
			sendMessageService.sendSms(phone, content);
		} catch (Exception e) {
			e.printStackTrace();
			stateCode = "0";  //发送短信失败
		}
		return stateCode;
	}

	/**
	 * 通过内容发送短信
	 * @param content
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/sendSmsByContent", method = RequestMethod.GET)
	public String sendSms(@RequestParam("content") String content) throws Exception {
		String stateCode = "1"; // 发短信成功
		// 获取短信接收手机号信息
		List<TmSmsAccept> list = sendMessageService.getSmsAcceptInfo();
		Long smsPhone = null;
		List<Long> tmSmsPhoneList = new ArrayList<Long>(); // 短信接收手机号集合
			for (TmSmsAccept tmSmsAccept : list) {
				smsPhone = tmSmsAccept.getSmsPhone();
				tmSmsPhoneList.add(smsPhone);
			}
			try {
				sendMessageService.sendSms(content,tmSmsPhoneList);
			} catch (Exception e) {
				e.printStackTrace();
				stateCode = "0";  //发送短信失败
			}
		return stateCode;
	}
	/**
	 * 
	 * 通过定时任务调用发送短信
	 * @param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/sendSmsByTimer", method = RequestMethod.GET)
	public String sendSms() throws Exception {

		List<TmKeepCountVo> tmKeepCountVos =  ttWinnerRecordService.queryTmKeepCountByDate();
		StringBuffer resultmsg = new StringBuffer();
		if(tmKeepCountVos!=null&&tmKeepCountVos.size()>0){
			for(TmKeepCountVo tmKeepCountVo:tmKeepCountVos){
				resultmsg.append(tmKeepCountVo.getRegionName()+":"+tmKeepCountVo.getProviceName()+" 数量:"+tmKeepCountVo.getQuerycount()+"\n");
			}
		}else{
			resultmsg.append("今天客户留资数量为0");
		}
		String content = resultmsg.toString(); // 发短信内容
		String stateCode = "1"; // 发短信成功
		//获取要发送短信的内容
		
		// 获取短信接收手机号信息
		List<TmSmsAccept> list = sendMessageService.getSmsAcceptInfo();
		Long smsPhone = null;
		List<Long> tmSmsPhoneList = new ArrayList<Long>(); // 短信接收手机号集合
		for (TmSmsAccept tmSmsAccept : list) {
			smsPhone = tmSmsAccept.getSmsPhone();
			tmSmsPhoneList.add(smsPhone);
		}
		try {
			sendMessageService.sendSms(content,tmSmsPhoneList);
		} catch (Exception e) {
			e.printStackTrace();
			stateCode = "0";  //发送短信失败
		}
		return stateCode;
	}
	
}
